package backend.model.admin;

import javax.persistence.*;

import backend.model.base.DBModel;
import backend.model.other.User;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;

@NamedQueries({ @NamedQuery(name = "findAllEmployees", query = "from Employee"),
                @NamedQuery(name = "findAllActiveEmployees", query = "from Employee where isActive = true"),
                @NamedQuery(name = "findAllActiveEmployeesByTeam", query = "from Employee where isActive = true and team.id = :teamId"),
                @NamedQuery(name = "findActiveEmployeeByIdentificationCard", query = "from Employee where isActive = true and identificationCard = :identificationCard"),
                @NamedQuery(name = "findEmployeeByFirstAndLastName", query = "from Employee where firstName = :firstName and lastName = :lastName"),
                @NamedQuery(name = "findAllEmployeesByForeman", query = "from Employee e where e.team.foreman.id = :foremanId ")})
@Entity
@Table(name = "employee")
public class Employee extends DBModel {

	@Id
	@GeneratedValue
	@Column(name = "id")
	private int id;

	@Column(name = "last_name")
	private String lastName;

	@Column(name = "first_name")
	private String firstName;

	@Column(name = "identification_card")
	private String identificationCard;

	@Column(name = "is_active")
	private boolean isActive;

    @ManyToOne
    @JoinColumn(name = "occupation_id")
    private Occupation occupation;

	@ManyToOne
	@JoinColumn(name = "team_id")
	private Team team;
	
	@OneToOne(fetch = FetchType.EAGER, mappedBy = "employee", cascade = CascadeType.ALL)
	private User user;

    @OneToOne(fetch = FetchType.EAGER, mappedBy = "employee", cascade = CascadeType.ALL)
    private Foreman foreman;

    @Override
    public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getIdentificationCard() {
		return identificationCard;
	}

	public void setIdentificationCard(String identificationCard) {
		this.identificationCard = identificationCard;
	}

	public boolean getIsActive() {
		return isActive;
	}

    public Occupation getOccupation() {
        return occupation;
    }

    public void setOccupation(Occupation occupation) {
        this.occupation = occupation;
    }

	public Team getTeam() {
		return team;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public void setIsActive(boolean isActive) {
		this.isActive = isActive;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

    public Foreman getForeman() {
        return foreman;
    }

    public void setForeman(Foreman foreman) {
        this.foreman = foreman;
    }

    @Override
    public String getDisplayLabel() {
        return lastName + " " + firstName;
    }

    @Override
    public int compareTo(Object o) {
        if (!(o instanceof Employee)) {
            return -1;
        } else {
            Employee otherEmployee = (Employee) o;
            return getDisplayLabel().compareTo(otherEmployee.getDisplayLabel());
        }
    }
}